package com.xbb.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.xbb.bean.Sidebar;
import com.xbb.bean.User;
import com.xbb.dao.SidebarMapperDao;
import com.xbb.dao.UserMapperDao;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xbb on 15/7/6.
 */
@Controller
public class loginController {

    @Resource
    private UserMapperDao dao;


    @Resource
    private SidebarMapperDao sdao;

    @RequestMapping("toLogin")
    public String checkPassword(String username,String password,Model model,HttpSession session){


        User user= dao.findByUsername(username);
        if(username == null){
            model.addAttribute("error","请先登录");
            return "../../jsp/login";
        }
        if(user!=null && user.getPassword().equals(password)){
            session.setAttribute("username",username);
            return "redirect:main/index.do";

        }else{
            model.addAttribute("error","账号或密码输入错误");
            return "../../jsp/login";
        }
    }

    @ResponseBody
    @RequestMapping("add_user")
    public Map<String, String> addUser(Model model){
      //Integer i= dao.addUser(user);
        Map<String, String> map = new HashMap<String, String>();
        int i=-1;
        if(i==1){
            map.put("mess","注册成功");
            return map;
        }else{
            map.put("mess","注册失败");
            return map;
        }
    }
}
